//
// Ishtar C++ Utility Library
//
// Task Facility - Work Private Header
//

#ifndef __ISHTAR_TASK_WORK_IMPL_H
#define __ISHTAR_TASK_WORK_IMPL_H

#include <Ishtar/Ishtar.h>

#if defined( ISHTAR_COMPILER_HAS_PRAGMA_ONCE )
#pragma once
#endif

#include <Ishtar/InstanceCounted.h>
#include <Ishtar/Memory/SingletonPooledObject.h>
#include <Ishtar/Task/TaskFwd.h>
#include <Ishtar/Task/Work.h>
#include <Ishtar/Task/WorkState.h>


namespace Ishtar
{

namespace Task
{

class WorkExecutorImpl;

///////////////////////////////////////////////////////////////////////////////
//
// Work
//

// TODO: replace this with <Ishtar/Facility.h>
static const Uint FACILITY_LONGEVITY_TASK = 0x80000001;

class WorkImpl : public InstanceCounted< WorkImpl >  // for debugging
               , public ReferenceCounted
               , public SingletonPooledObject< WorkImpl, FACILITY_LONGEVITY_TASK + 1 >
{
public:

    WorkImpl( const std::string& name, const WorkFunction& function );

    void Start( WorkExecutorImpl* executor );

    void Run();


    //
    // Cancellation
    //

    Bool Cancel();


    //
    // Accessors
    //

    std::string Name() const { return m_name; }


private:

    //
    // Internal Functions
    //

    void DoCancel();


    //
    // Data Members
    //

    std::string  m_name;
    WorkFunction m_function;

    WorkExecutorImpl* m_executor;

    typedef WorkStateMachine State;
    State m_state;

};

typedef boost::intrusive_ptr< WorkImpl > WorkImplPtr;


///////////////////////////////////////////////////////////////////////////////

} // namespace Task

} // namespace Ishtar

#endif // __ISHTAR_TASK_WORK_IMPL_H
